package com.exes;

import cn.edu.hfut.dmic.webcollector.model.CrawlDatum;
import cn.edu.hfut.dmic.webcollector.model.CrawlDatums;
import cn.edu.hfut.dmic.webcollector.model.Page;
import cn.edu.hfut.dmic.webcollector.plugin.rocks.BreadthCrawler;
import com.db.CacheDb;
import com.db.ExcelUtils;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.Locator;
import com.microsoft.playwright.Playwright;
import com.model.DocumentInfo;
import lombok.extern.slf4j.Slf4j;

/**
 * 速度太慢，暂不使用
 */
@Slf4j
public class MainExecutor extends BreadthCrawler {
    private String entryIndex = "https://kns.cnki.net/kns8/defaultresult/index";
    private String path = "E:\\tmp\\test.xlsx";



    public MainExecutor(String crawlPath, boolean autoParse) {
        super(crawlPath, autoParse);
    }

    public void run(){
        this.addSeed(entryIndex);

        setThreads(50);
        getConf().setTopN(100);

        try {
            this.start(1);
        } catch (Exception e) {
            log.error("crawler start exception");
            e.printStackTrace();
        }
        System.out.println("执行完成");
        System.out.println(CacheDb.data);
        ExcelUtils.writeToFile(CacheDb.data, path);
    }

    @Override
    public void execute(CrawlDatum datum, CrawlDatums next) throws Exception {
        super.execute(datum, next);
        String keyWord = "计算机";
        String host = "https://kns.cnki.net";
        try (Playwright playwright = Playwright.create()) {
            Browser browser = playwright.webkit().launch();
            com.microsoft.playwright.Page page = browser.newPage();
            page.navigate(datum.url());
            Locator locator = page.locator(" #txt_search");
            locator.fill(keyWord);
            Locator searchBtn = page.locator(".search-btn");
            searchBtn.click();
            page.waitForTimeout(1000 * 2);
            Locator trs = page.locator("#gridTable .result-table-list tbody tr");
            for(int i = 0; i < trs.count(); i++){
                Locator line = trs.nth(i);
                Locator tds = line.locator("td");
                DocumentInfo documentInfo = DocumentInfo.builder()
                        .title(tds.nth(1).locator("a").nth(0).innerText())
                        .detailLink(host + tds.nth(1).locator("a").nth(0).getAttribute("href"))
                        .author(tds.nth(2).innerText())
                        .source(tds.nth(3).locator("a").nth(0).innerText())
                        .releaseTime(tds.nth(4).innerText())
                        .datasource(tds.nth(5).innerText())
                        .downloadCount(Integer.parseInt(tds.nth(7).locator("a").innerText()))
                        .build();
                CacheDb.data.add(documentInfo);
            }
        }finally {

        }
    }

    @Override
    public void visit(Page page, CrawlDatums crawlDatums) {

    }

    public static void main(String[] args) {

        MainExecutor mainExecutor = new MainExecutor("crawl", false);
        mainExecutor.run();
    }


}
